package com.ts.animal.controller;

import com.ts.animal.constant.AnimalConstant;
import com.ts.animal.domain.BaseStationDO;
import com.ts.animal.service.BaseStationService;
import com.ts.common.redis.RedisUtil;
import com.ts.common.utils.*;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * bs_base_station 基站
 *
 * @author bobby
 * @email bobby@126.com
 * @date 2022-09-02 16:09:19
 */

@Controller
@RequestMapping("/animal/baseStation")
public class BaseStationController {
    @Autowired
    private BaseStationService baseStationService;

    @GetMapping()
    @RequiresPermissions("animal:baseStation:baseStation")
    String BaseStation() {
        return "animal/baseStation/baseStation";
    }

    @ResponseBody
    @RequestMapping("/list")
    @RequiresPermissions("animal:baseStation:baseStation")
    public PageUtils list(@RequestParam Map<String, Object> params) {
        //查询列表数据
        PageUtils pageUtils = new PageInfos(params) {
            public List inject(Map params) {
                return baseStationService.list(params);
            }
        }.query().lowerUnderscore();

        for (Map<String, Object> item : (List<Map<String, Object>>) pageUtils.getRows()) {
            String status = RedisUtil.get(AnimalConstant.REDIS_BASE_STATION_STATUS_KEY + item.get("baseStationNum"));
            if (StringUtils.isNotEmpty(status)) {
                item.put("status", status);
            }
        }
        return pageUtils;
    }

    @ResponseBody
    @RequestMapping("/dept/list")
    public PageUtils deptList(@RequestParam Map<String, Object> params) {
        //查询列表数据
        Query query = new Query(params);
        List<BaseStationDO> baseStationList = baseStationService.deptSelectBaseStationList(query);
        for (BaseStationDO item : baseStationList) {
            String status = RedisUtil.get(AnimalConstant.REDIS_BASE_STATION_STATUS_KEY + item.getBaseStationNum());
            if (StringUtils.isNotEmpty(status)) {
                item.setStatus(status);
            }
        }
        PageUtils pageUtils = new PageUtils(baseStationList, baseStationList.size());
        return pageUtils;
    }

    @GetMapping("/add")
    @RequiresPermissions("animal:baseStation:add")
    String add() {
        return "animal/baseStation/add";
    }

    @GetMapping("/edit/{baseStationNum}")
    @RequiresPermissions("animal:baseStation:edit")
    String edit(@PathVariable("baseStationNum") String baseStationNum, Model model) {
        BaseStationDO baseStation = baseStationService.get(baseStationNum);
        model.addAttribute("baseStation", baseStation);
        return "animal/baseStation/edit";
    }

    /**
     * 保存
     */
    @ResponseBody
    @PostMapping("/save")
    @RequiresPermissions("animal:baseStation:add")
    public Result save(BaseStationDO baseStation) {
        Result result = Result.ok();
        baseStationService.save(result, baseStation);
        return result;
    }

    /**
     * 保存
     */
    @ResponseBody
    @PostMapping("/dept/relateStation/{deptId}")
    public Result deptSave(@PathVariable long deptId, @RequestBody String[] baseStationNumList) {
        Result result = Result.ok();
        baseStationService.relateStation(result, deptId, baseStationNumList);
        return result;
    }

    /**
     * 修改
     */
    @ResponseBody
    @RequestMapping("/update")
    @RequiresPermissions("animal:baseStation:edit")
    public Result update(BaseStationDO baseStation) {
        Result result = Result.ok();
        baseStationService.update(result, baseStation);
        return result;
    }

    /**
     * 删除
     */
    @PostMapping("/remove")
    @ResponseBody
    @RequiresPermissions("animal:baseStation:remove")
    public Result remove(String baseStationNum) {
        Result result = Result.ok();
        baseStationService.remove(result, baseStationNum);
        return result;
    }

    /**
     * 删除
     */
    @PostMapping("/batchRemove")
    @ResponseBody
    @RequiresPermissions("animal:baseStation:batchRemove")
    public Result remove(@RequestParam("ids[]") String[] baseStationNums) {
        Result result = Result.ok();
        baseStationService.batchRemove(result, baseStationNums);
        return result;
    }

    /**
     * 清理养殖户下的标签
     */
    @PostMapping("/clearDeptLabel")
    @ResponseBody
    @RequiresPermissions("animal:baseStation:clearDeptLabel")
    public Result clearDeptLabel(long deptId) {
        baseStationService.batchRfidLabelData(deptId);
        return Result.ok();
    }
}
